package com.example.shorg.controller;

import com.example.shorg.convert.OrderForm2OrderDTOConverter;
import com.example.shorg.dto.OrderDTO;
import com.example.shorg.enums.OrderStatusEnum;
import com.example.shorg.enums.ResultEnum;
import com.example.shorg.exception.ShorgException;
import com.example.shorg.form.OrderForm;
import com.example.shorg.service.BuyerService;
import com.example.shorg.service.OrderService;
import com.example.shorg.service.impl.BuyerServiceImpl;
import com.example.shorg.service.impl.OrderServiceImpl;
import com.example.shorg.utils.ResultVOUtils;
import com.example.shorg.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {

    @Autowired
    private OrderServiceImpl orderService;

    @Autowired
    private BuyerServiceImpl buyerService;

    //创建订单
    @PostMapping("/createOrder")
    public ResultVO<Map<String, String>> createOrder(@Valid OrderForm orderForm, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("[创建订单]参数不正确，", orderForm);
            throw new ShorgException(ResultEnum.PARAM_ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        OrderDTO convert = OrderForm2OrderDTOConverter.convert(orderForm);
        if(CollectionUtils.isEmpty(convert.getOrderDetailList())){
            log.error("【创建订单】购物车不能为空");
            throw new ShorgException(ResultEnum.CART_EMPTY);
        }
        OrderDTO orderDTO = orderService.create(convert);
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderDTO.getOrderId());
        return ResultVOUtils.success(map);
    }

    //订单列表
    @GetMapping("/orderList")
    public ResultVO<OrderDTO> orderList(@RequestParam("openid") String openid,
                                        @RequestParam(value = "page", defaultValue = "0") Integer page,
                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        if(StringUtils.isEmpty(openid)){
            log.error("【查询订单列表】openid为空");
            throw new ShorgException(ResultEnum.PARAM_ERROR);
        }
        PageRequest request = new PageRequest(page, pageSize);
        Page<OrderDTO> list = orderService.findList(openid, request);
        return ResultVOUtils.success(list.getContent());
    }

    //订单详情
    @PostMapping("orderDetail")
    public ResultVO<OrderDTO> orderDetail(@RequestParam("openid") String openid, @RequestParam("orderid") String orderid){

        //TODO  不安全的操作
//        OrderDTO orderDTO = orderService.findOne(orderid);
//        if(orderDTO == null){
//            log.error("订单不存在");
//            throw new ShorgException(ResultEnum.ORDER_NOT_EXIST);
//        }
        OrderDTO orderOne = buyerService.findOrderOne(openid, orderid);
        return ResultVOUtils.success(orderOne);
    }

    //取消订单
    @PostMapping("cancleOrder")
    public ResultVO cancleOrder(@RequestParam("openid") String openid, @RequestParam("orderid") String orderid){
        //TODO  不安全做法
        //将订单查询出来
//        OrderDTO orderDTO = orderService.findOne(orderid);
//
//        if(orderDTO == null){
//            log.error("订单不存在");
//            throw new ShorgException(ResultEnum.ORDER_NOT_EXIST);
//        }
//        orderService.cancle(orderDTO);
        buyerService.cancleOrder(openid, orderid);
        return ResultVOUtils.success();
    }

}
